package com.atguigu.gmall.activity.service.impl;

import com.atguigu.gmall.activity.model.SeckillGoods;
import com.atguigu.gmall.activity.mapper.SeckillGoodsMapper;
import com.atguigu.gmall.activity.service.SeckillGoodsService;
import com.atguigu.gmall.common.constant.RedisConst;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  业务实现类
 *
 * @author atguigu
 * @since 2023-08-15
 */
@Service
public class SeckillGoodsServiceImpl extends ServiceImpl<SeckillGoodsMapper, SeckillGoods> implements SeckillGoodsService {

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 查询当日参与秒杀商品
     *
     * @return
     */
    @Override
    public List<SeckillGoods> getSeckillGoods() {
        BoundHashOperations<String, String, SeckillGoods> seckillHashOps = getSeckillGoodsHashOpes();
        return seckillHashOps.values();
    }


    /**
     * 返回操作秒杀商品对象
     *
     * @return
     */
    private BoundHashOperations<String, String, SeckillGoods> getSeckillGoodsHashOpes() {
        String seckillKey = RedisConst.SECKILL_GOODS;
        BoundHashOperations<String, String, SeckillGoods> seckillHashOps = redisTemplate.boundHashOps(seckillKey);
        return seckillHashOps;
    }

    /**
     * 查询秒杀商品详情
     *
     * @param skuId
     * @return
     */
    @Override
    public SeckillGoods getSecGoodsById(Long skuId) {
        BoundHashOperations<String, String, SeckillGoods> hashOpes = getSeckillGoodsHashOpes();
        String hashKey = skuId.toString();
        Boolean flag = hashOpes.hasKey(hashKey);
        if (flag) {
            return hashOpes.get(hashKey);
        }
        return null;
    }
}
